﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using ExamSharedClasses;
using ExamClient;
using System.ComponentModel;
using System.Windows.Controls.Primitives;

namespace ExamClient
{
    /// <summary>
    /// (l) Das zuerst angezeigte Fenster, welches verschiedene grundlegende
    /// Funktionen anbieten soll.
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            InitialThingsToDo();
        }

        public void InitialThingsToDo()
        {
            App.MainWindow = this;
            App.Session = new Session();
            App.UserAddress = new UserAddress();
            App.Password = new Password();
            //App.LogStringQueue = new Queue<string>();
            App.Display("Client (Version " + App.VERSION + ") gestartet...");

            App.messageCountdown = -1;
            App.DispatcherTimer = new DispatcherTimer();
            App.DispatcherTimer.Tick += new EventHandler(UpdateGUI);
            App.DispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); // 1 sek
            App.DispatcherTimer.Start();
        }

        public void UpdateGUI(object sender, EventArgs e)
        {

            if (App.ServerConnection != null && App.ServerConnection.IsLoggedIn)
            {
                // (l) Timer-Methoden oder so
            }


            // Nach 5sec Statustext ausblenden
            if (messageTextBlock.Text.Length > 0)
            {
                if (App.messageCountdown > 0)
                    App.messageCountdown--;
                else if (App.messageCountdown == 0)
                {
                    messageTextBlock.Text = "";
                    App.messageCountdown--;
                }
                else
                    App.messageCountdown = 5;
            }
        }

        void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                App.ServerConnection.Logout();
            }
            catch { }
            finally
            {
                this.Close();
            }
        }

        /// <summary>
        /// Ändert verschiedene Elemente nach erfolgreicher Anmeldung
        /// </summary>
        public void ChangePagesAfterLogin()
        {
            
            App.LoginBar.ChangeLoginToLogoutBar();
            App.MenuBar.ChangeAccordingToGroup();
            App.MainWindow.Title += " - " + App.UserAddress.UserGroup.GroupName + "-Modus";
            // Hier erste aufzurufende Seite nach dem Login eintragen:
            // Fehlt noch automatische Unterscheidung User, Trainer, Admin
            this.center.Content = new SummaryPage();
            
        }

        /// <summary>
        /// Methode zum Setzen der Nachricht in der StatusBar
        /// </summary>
        /// <param name="message"></param>
        public void SetStatusBarMessage(String message)
        {
            messageTextBlock.Text = message;
        }
    }
}
